package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;

public class Selector10 extends AxdrType {
    private byte[] dataCode = null;
    private Unsigned8 recn = null;
    private MS ms = null;

    public Selector10() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public Unsigned8 getRecn() {
        return recn;
    }

    public MS getMs() {
        return ms;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(Unsigned8 recn, MS ms) {
        this.recn = recn;
        this.ms = ms;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.recn = new Unsigned8();
        this.recn.setConst();
        codeLength += this.recn.decode(input);
        this.ms = new MS();
        codeLength += this.ms.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            byte[] data = this.dataCode;
            codeLength = data.length;
            for (int i = data.length - 1; i >= 0; i--) {
                output.write(data[i]);
            }
        } else {
            if (this.recn != null && this.ms != null) {
                codeLength = 0;
                codeLength += this.ms.encode(output);
                codeLength += this.recn.encode(output);
            } else {
                throw new RuntimeException("recn or ms is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    @Override
    public String toString() {
        if (this.recn != null && this.ms != null) {
            return "sequence: {" + "recn: " + this.recn + ", ms: " + this.ms + "}";
        } else {
            return "recn or ms is null";
        }
    }
}



